/*
* 表单提交验证
* */
var urlUAA = "";

var AddUrl = "/AddcardServlet";
var UpdateUrl = "/UpdateCardAmountServlet";


$(function () {
    //  在ajax启动    初始模态框
    initRechargerule();
})

// 设置模态框 监听事件
$("#myModal").on('hide.bs.modal', function () {

})

function initRechargerule() {


    // 本来是通过 ajax 请求获取的，为了给大家延时 就省略ajax  直接解析了json
    $.ajax({
        url: "/GetRechargeruleAllServlet",
        type: 'post',
        dataType: 'json',
        success: function (result) {
            var res = result.data;
            for (var i = 0; i < res.length; i++) {
                var opt = $("<option value='" + res[i].id + "'>" + res[i].name + "</option>");
                $("#rechargeruleName").append(opt);
            }
        }
    })

    // var res = JSON.parse("[{\"id\":1,\"roleName\":\"管理员\",\"grade\":1,\"description\":\"管理员\",\"status\":1},{\"id\":2,\"roleName\":\"吧台\",\"grade\":2,\"description\":\"小卖部\",\"status\":1},{\"id\":3,\"roleName\":\"前台\",\"grade\":3,\"description\":\"前台\",\"status\":1},{\"id\":4,\"roleName\":\"收银员\",\"grade\":4,\"description\":\"收银员\",\"status\":1},{\"id\":5,\"roleName\":\"店长\",\"grade\":5,\"description\":\"店长\",\"status\":1},{\"id\":7,\"roleName\":\"新增职位\",\"description\":\"新增的职位\",\"status\":1},{\"id\":8,\"roleName\":\"保洁\",\"description\":\"保洁--负责用泳池消毒\",\"status\":1}]");


}


// function addcard() {
//
//     $("#myModalLabel").text("添加员工");
//     // 调用显示模态框
//     $("#myModal").modal('show');
//     $("#cardForm")[0].reset();
//     urlUAA= AddUrl;
//
//
//
// }


function updatecard(row) {

    $("#myModalLabel").text("充值");
    $("#cardId").val(row.cardId);
    $("#amount").val(row.amount);
    $("#credit").val(row.credit);
    $("#idCard").val(row.idCard);
    $("#coefficient").val(row.coefficient);
    urlUAA = UpdateUrl;
    // 调用显示模态框
    $("#myModal").modal('show');

}


function aaa() {
    // 当点击提交校验输入框

    var bootstrapValidator = $("#cardForm").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (bootstrapValidator.isValid()) {
        toastr['success']("校验成功");
        $.ajax({
            url: urlUAA,
            type: 'post',
            data: {
                cardId: $("#cardId").val(),
                amount: $("#amount").val(),
                // credit: $("#credit").val(),

                coefficient: $("#coefficient").val(),
                rechargeruleName:$("#rechargeruleName").val(),
                staffId:window.localStorage.staffId,
                money:$("#money").val()
            },
            dataType: 'json',
            success: function (result) {
                if (result.status > 0) {
                    toastr['success']("操作成功");
                    $("#myModal").modal('hide');
                    $("#cardList").bootstrapTable('refresh');
                } else {
                    toastr['error']("操作失败");
                }
            }
        })
    } else {
        toastr['error']("校验失败");


        // $("#oderForm")[0].reset();

    }
}

/**
 * 关闭模态框
 */
$("#myModal").on('hide.bs.modal', function () {
    //移除上次的校验配置
    $("#cardForm").data('bootstrapValidator').resetForm();
    $("#cardForm")[0].reset();
})

/*
* 初始化表单验证
* */
$("#cardForm").bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        money: {
            validators: {
                notEmpty: {
                    message: "充值金额不能为空"
                }
            }
        }
    }
});
